package org.eenie.kdassistant.utils.zzt_encode;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;

/**
 * Created by Eenie on 2017/8/14.
 * Email: eeniework@gmail.com
 * Des:
 */

public abstract class BaseHex {
    protected PrintStream a;

    public BaseHex() {
    }

    private byte[] c(ByteBuffer var1) {
        byte[] var2 = (byte[])null;
        byte[] var3 = var2;
        byte[] var4;
        if(var1.hasArray()) {
            var4 = var1.array();
            var3 = var2;
            if(var4.length == var1.capacity()) {
                var3 = var2;
                if(var4.length == var1.remaining()) {
                    var1.position(var1.limit());
                    var3 = var4;
                }
            }
        }

        var4 = var3;
        if(var3 == null) {
            var4 = new byte[var1.remaining()];
            var1.get(var4);
        }

        return var4;
    }

    protected abstract int a();

    protected int a(InputStream var1, byte[] var2) throws IOException {
        int var3 = 0;

        int var4;
        while(true) {
            if(var3 >= var2.length) {
                var4 = var2.length;
                break;
            }

            int var5 = var1.read();
            var4 = var3;
            if(var5 == -1) {
                break;
            }

            var2[var3] = (byte)((byte)var5);
            ++var3;
        }

        return var4;
    }

    public String a(ByteBuffer var1) {
        return this.a(this.c(var1));
    }

    public String a(byte[] var1) {
        ByteArrayOutputStream var2 = new ByteArrayOutputStream();
        ByteArrayInputStream var4 = new ByteArrayInputStream(var1);

        try {
            this.a((InputStream)var4, (OutputStream)var2);
            String var5 = var2.toString("8859_1");
            return var5;
        } catch (Exception var3) {
            throw new Error("CharacterEncoder.encode internal error");
        }
    }

    public void a(InputStream var1, OutputStream var2) throws IOException {
        byte[] var3 = new byte[this.b()];
        this.a(var2);

        while(true) {
            int var4 = this.a(var1, var3);
            if(var4 == 0) {
                break;
            }

            this.a(var2, var4);

            for(int var5 = 0; var5 < var4; var5 += this.a()) {
                if(this.a() + var5 <= var4) {
                    this.a(var2, var3, var5, this.a());
                } else {
                    this.a(var2, var3, var5, var4 - var5);
                }
            }

            if(var4 < this.b()) {
                break;
            }

            this.c(var2);
        }

        this.b(var2);
    }

    protected void a(OutputStream var1) throws IOException {
        this.a = new PrintStream(var1);
    }

    protected void a(OutputStream var1, int var2) throws IOException {
    }

    protected abstract void a(OutputStream var1, byte[] var2, int var3, int var4) throws IOException;

    public void a(ByteBuffer var1, OutputStream var2) throws IOException {
        this.a(this.c(var1), var2);
    }

    public void a(byte[] var1, OutputStream var2) throws IOException {
        this.a((InputStream)(new ByteArrayInputStream(var1)), (OutputStream)var2);
    }

    protected abstract int b();

    public String b(ByteBuffer var1) {
        return this.b(this.c(var1));
    }

    public String b(byte[] var1) {
        ByteArrayOutputStream var2 = new ByteArrayOutputStream();
        ByteArrayInputStream var4 = new ByteArrayInputStream(var1);

        try {
            this.b((InputStream)var4, var2);
        } catch (Exception var3) {
            throw new Error("CharacterEncoder.encodeBuffer internal error");
        }

        return var2.toString();
    }

    public void b(InputStream var1, OutputStream var2) throws IOException {
        byte[] var3 = new byte[this.b()];
        this.a(var2);

        int var4;
        do {
            var4 = this.a(var1, var3);
            if(var4 == 0) {
                break;
            }

            this.a(var2, var4);

            for(int var5 = 0; var5 < var4; var5 += this.a()) {
                if(this.a() + var5 <= var4) {
                    this.a(var2, var3, var5, this.a());
                } else {
                    this.a(var2, var3, var5, var4 - var5);
                }
            }

            this.c(var2);
        } while(var4 >= this.b());

        this.b(var2);
    }

    protected void b(OutputStream var1) throws IOException {
    }

    public void b(ByteBuffer var1, OutputStream var2) throws IOException {
        this.b(this.c(var1), var2);
    }

    public void b(byte[] var1, OutputStream var2) throws IOException {
        this.b((InputStream)(new ByteArrayInputStream(var1)), var2);
    }

    protected void c(OutputStream var1) throws IOException {
        this.a.println();
    }
}
